package com.cskaoyan.shopping.service.impl;

import com.cskaoyan.shopping.constants.GlobalConstants;
import com.cskaoyan.shopping.converter.ContentConverter;
import com.cskaoyan.shopping.converter.ItemCatConverter;
import com.cskaoyan.shopping.converter.PanelConverter;
import com.cskaoyan.shopping.dal.entitys.ItemCat;
import com.cskaoyan.shopping.dal.entitys.Panel;
import com.cskaoyan.shopping.dal.entitys.PanelContent;
import com.cskaoyan.shopping.dal.entitys.PanelContentItem;
import com.cskaoyan.shopping.dal.persistence.ItemCatMapper;
import com.cskaoyan.shopping.dal.persistence.ItemMapper;
import com.cskaoyan.shopping.dal.persistence.PanelContentMapper;
import com.cskaoyan.shopping.dal.persistence.PanelMapper;
import com.cskaoyan.shopping.dto.ItemCatDto;
import com.cskaoyan.shopping.dto.PanelContentDto;
import com.cskaoyan.shopping.dto.PanelDto;
import com.cskaoyan.shopping.service.IHomeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.List;

/**
 * @author gzh
 * @version 1.0
 * @date 2022/12/04 21:50:52
 */
@Service
public class IHomeServiceImpl implements IHomeService {
    @Autowired
    PanelMapper panelMapper;

    @Autowired
    PanelContentMapper panelContentMapper;

    @Autowired
    ItemMapper itemMapper;

    @Autowired
    PanelConverter panelConverter;

    @Autowired
    ItemCatMapper itemCatMapper;

    @Autowired
    ItemCatConverter itemCatConverter;

    @Override
    public List<PanelDto> homepage() {
        List<Panel> panels = panelMapper.selectByExample(null);
        for (Panel panel : panels) {

            List<PanelContentItem> panelContentItems = panelContentMapper.selectPanelContentAndProductWithPanelId(panel.getId());
            panel.setPanelContentItems(panelContentItems);

        }
        List<PanelDto> panelDtos = panelConverter.panels2PanelDtos(panels);

        return panelDtos;
    }


    @Override
    public List<PanelContentDto> navigation() {

        Example example = new Example(PanelContent.class);
        example.createCriteria().andEqualTo("panelId", GlobalConstants.HEADER_PANEL_ID);
        List<PanelContent> panelContents = panelContentMapper.selectByExample(example);
        List<PanelContentDto> panelContentDtos = panelConverter.panelContents2PanelContentDtos(panelContents);

        return panelContentDtos;
    }



    @Override
    public List<ItemCatDto> categories() {
        List<ItemCat> itemCats = itemCatMapper.selectByExample(null);
        List<ItemCatDto> itemCatDtos = itemCatConverter.itemCats2ItemCatDtos(itemCats);

        return itemCatDtos;
    }
}
